package com.mm.dss.demo;

import android.Manifest;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mm.dss.demo.Interface.OkGoUtilsInterFace;
import com.mm.dss.demo.base.BaseActivity;
import com.mm.dss.demo.bean.PxBean;
import com.mm.dss.demo.bean.YjxxBean;
import com.mm.dss.demo.tools.Constances;
import com.mm.dss.demo.tools.OkgoUtils;
import com.mm.dss.demo.tools.SharedPreferencedUtils;
import com.mm.dss.demo.tools.SwipeRefreshHelper;
import com.mm.dss.demo.tools.WebServiceUtils;
import com.mm.dss.demo.tools.WeiboDialogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

import static com.mm.dss.demo.tools.Constances.wnjianurl;

/*
* 学习培训
* */
public class XxpxActivity extends BaseActivity {
    RelativeLayout laytout_back;
    TextView txt_title;
    RecyclerView recyclerView;
    BaseRecyclerAdapter<PxBean> baseRecyclerAdapter;
    List<PxBean> pxBeanlist=new ArrayList<>();
    private Dialog mWeiboDialog;
    private SwipeRefreshLayout swipregresh;

    private void setnet(boolean IsDialog) {
        if (pxBeanlist.size()>0){
            pxBeanlist.clear();
        }
        if (IsDialog) {
            mWeiboDialog = WeiboDialogUtils.
                    createLoadingDialog(XxpxActivity.this,
                            "加载中...");
        }
        Map<String,String> map=new HashMap<>();
       // map.put("userid",SharedPreferencedUtils.getString(XxpxActivity.this,"username"));
        WebServiceUtils.callWebService(Constances.webhosturl, "N13_GetXXZLList", null, new WebServiceUtils.WebServiceCallBack() {
            @Override
            public void callBack(SoapObject result) {
                Log.i("学习资料",result.getProperty(0).toString());
                if (IsDialog) {
                    WeiboDialogUtils.closeDialog(mWeiboDialog);
                }
                finishRefresh();
                try {
                    JSONArray array=new JSONArray(result.getProperty(0).toString());
                    Gson gson=new Gson();
                    for (int i=0;i<array.length();i++){
                        PxBean pxBean=gson.fromJson(array.optJSONObject(i).toString(),PxBean.class);
                        pxBeanlist.add(pxBean);
                    }
                    baseRecyclerAdapter.setData(pxBeanlist);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxpx);
        txt_title= (TextView) findViewById(R.id.txt_title);
        laytout_back= (RelativeLayout) findViewById(R.id.layout_back);
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
        txt_title.setText("学习培训");
        laytout_back.setVisibility(View.VISIBLE);
        swipregresh= (SwipeRefreshLayout) findViewById(R.id.swipregresh);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        initdata();
        initSwipeRefresh();
    }

    protected void initdata() {
        baseRecyclerAdapter=new BaseRecyclerAdapter<PxBean>(this,null,R.layout.item_pxzlview) {
            @Override
            protected void convert(BaseViewHolder helper, final PxBean item) {
                helper.setText(R.id.title,item.getBIAOTI());
                helper.setText(R.id.time,item.getSHIJIAN());
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("文件地址",wnjianurl+item.getLIANJIE());

                        String[] perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE};
                        if (!EasyPermissions.hasPermissions(XxpxActivity.this, perms)) {
                            EasyPermissions.requestPermissions(XxpxActivity.this, "需要访问手机存储权限！", 10086, perms);
                        } else {
                            FileDisplayActivity.show(XxpxActivity.this, item.getLIANJIE());
                        }

                    }
                });

            }
        };
        baseRecyclerAdapter.openLoadAnimation(false);
        recyclerView.setAdapter(baseRecyclerAdapter);
      setnet(true);

    }

    public void onclick(View view){
        switch (view.getId()){
            case R.id.layout_back:
                finish();
                break;
        }
    }
    /**
     * 初始化下拉刷新
     */
    private void initSwipeRefresh() {
        if (swipregresh != null) {
            SwipeRefreshHelper.init(swipregresh, new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    setnet(false);

                }
            });
        }
    }

    /**
     * 关闭刷新
     */
    public void finishRefresh() {
        if (swipregresh != null) {
            swipregresh.setRefreshing(false);
        }
    }
}
